package cn.hp.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

//用户认证（登录）
public class TestApp {

	public static void main(String[] args) {
		String username = "zhangsan1";
		String password = "123456";
		
		//1  创建SercurityManager工厂
		Factory<SecurityManager>factory = new IniSecurityManagerFactory("classpath:shiro.ini");
		//2  从工厂中获取SecurityManager
		SecurityManager securityManager = factory.getInstance();
		//3  把当前的SecurityManager绑定到当前线程中
		SecurityUtils.setSecurityManager(securityManager);
		//4  获取Subject主体
		Subject subject = SecurityUtils.getSubject();
		//5  封装用户名和密码
		UsernamePasswordToken token = new UsernamePasswordToken(username,password);
		//6  进行登录认证
		try {
			subject.login(token);
			System.out.println("是否认证成功："+subject.isAuthenticated());
		} catch (AuthenticationException e) {
			System.out.println("用户名或密码错误！");
		}
		/*try {
			subject.login(token);
			System.out.println("是否认证成功："+subject.isAuthenticated());
		} catch (IncorrectCredentialsException e) {//这个异常代表密码错误
			System.out.println("密码不正确");
		} catch (UnknownAccountException e) {
			System.out.println("用户名不正确");
		}*/
		
	}
}
